//
//  GYHome.m
//  GoodLuck
//
//  Created by apple on 2018/5/2.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYHome.h"
#import "GYNetWorkTools.h"
#import "GYGoods.h"
#import "GYBanner.h"
#import "GYNewGoods.h"

@implementation GYHome

+ (void)loadHomeData:(void (^)(GYHome * _Nullable, NSError * _Nullable))finish {
    [EasyLodingView showLodingText:@"正在加载..." config:^EasyLodingConfig *{
        EasyLodingConfig *config = [EasyLodingConfig shared];
        config.showOnWindow = YES;
        config.superReceiveEvent = NO;
        
        return config;
    }];
    [GYNetWorkTools requestHomeData:^(NSDictionary *obj) {
        //NSLog(@"obj=========%@",obj);
        int code = [[obj objectForKey:@"code"] intValue];
        if (code == 200) {
            [EasyLodingView hidenLoingInView:[UIApplication sharedApplication].keyWindow];
            NSArray *datas = [obj objectForKey:@"datas"];
            GYHome *home = [[GYHome alloc] initWithDic:datas];
            finish(home,nil);
            
            //1.把首页的数据用数据库缓存起来
            BOOL isSuccess = [datas writeToFile:[NSString CachesDir:@"homeData.plist"] atomically:YES];
            if (isSuccess) {
                NSLog(@"缓存成功...");
            }else {
                NSLog(@"缓存失败...");
            }
        }else {
            finish(nil,nil);
            [EasyLodingView hidenLoingInView:[UIApplication sharedApplication].keyWindow];
            [EasyTextView showText:@"亲，您的手机网络不太顺畅喔..."];
        }
    } error:^(NSError *error) {
        finish(nil,error);
        [EasyLodingView hidenLoingInView:[UIApplication sharedApplication].keyWindow];
        [EasyTextView showText:@"亲，您的手机网络不太顺畅喔..."];
    }];
}

+ (void)loadSpecial:(NSString *)special_id finish:(void (^)(GYHome * _Nullable, NSError * _Nullable))finish {
    [EasyLodingView showLodingText:@"正在加载..." config:^EasyLodingConfig *{
        EasyLodingConfig *config = [EasyLodingConfig shared];
        config.showOnWindow = YES;
        config.superReceiveEvent = NO;
        
        return config;
    }];
    [GYNetWorkTools requestHomeSpecial:special_id success:^(NSDictionary *obj) {
        //NSLog(@"obj===============%@",obj);
        int code = [[obj objectForKey:@"code"] intValue];
        if (code == 200) {
            [EasyLodingView hidenLoingInView:[UIApplication sharedApplication].keyWindow];
            NSArray *datas = [obj objectForKey:@"datas"];
            GYHome *home = [[GYHome alloc] initWithDic:datas];
            finish(home,nil);
        }else {
            finish(nil,nil);
            [EasyLodingView hidenLoingInView:[UIApplication sharedApplication].keyWindow];
            [EasyTextView showText:@"亲，您的手机网络不太顺畅喔..."];
        }

    } error:^(NSError *error) {
        [EasyLodingView hidenLoingInView:[UIApplication sharedApplication].keyWindow];
        [EasyTextView showText:@"亲，您的手机网络不太顺畅喔..."];
    }];
}

+ (void)loadCachesHomeData:(void (^)(GYHome * _Nullable))finsih {
    NSArray *source = [[NSArray alloc] initWithContentsOfFile:[NSString CachesDir:@"homeData.plist"]];
    GYHome *home = [[GYHome alloc] initWithDic:source];
    finsih(home);
}

- (instancetype)initWithDic:(NSArray *)datas {
    self = [super init];
    if (self) {
        self.home_news = [NSMutableArray array];
        for (NSDictionary *dic in datas) {
            if ([[dic allKeys] containsObject:@"home1"]) {
                //新品
                NSDictionary *home1 = [dic objectForKey:@"home1"];
                GYNewGoods *newGoods = [[GYNewGoods alloc] initWithDic:home1];
                
                [self.home_news addObject:newGoods];
                
            }else if ([[dic allKeys] containsObject:@"adv_list"]) {
                //banner
                NSDictionary *adv_list = [dic objectForKey:@"adv_list"];
                NSArray *item = [adv_list objectForKey:@"item"];
                
                self.home_banners = [GYBanner jsonDataWith:item];
                
        
            }else if ([[dic allKeys] containsObject:@"goods"]) {
                
                //商品
                NSDictionary *goods = [dic objectForKey:@"goods"];
                NSArray *item = [goods objectForKey:@"item"];
                
                self.goodsSectionTitle = [goods objectForKey:@"title"];
                self.home_Goods = [GYGoods loadGoods:item];
            }
        }
    }
    
    return self;
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
    
}


@end
